/**
 * Created by Ghost on 2014/10/14 0014.
 */

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.CellFormat;
import jxl.write.*;

public class ReadXLS {
    public HashMap<String,Integer> readXLSSrc(String fileDir){
        HashMap<String,Integer> names = new HashMap<String, Integer>();
        try {
            Workbook book = Workbook.getWorkbook(new File(fileDir));
            //  获得第一个工作表对象
            Sheet sheet = book.getSheet(0);
            //  得到第一列第一行的单元格

            for(int i=3;i<=259;i++){
                Cell cell1 = sheet.getCell(1, i);

                Cell cell2 = sheet.getCell(0,i);
                String result = cell1.getContents();
                System.out.println(result);
                if(result.length()>0){
                    int num = Integer.parseInt(cell2.getContents());
                    names.put(result,num);
                }
            }

            book.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return names;
    }
    public ArrayList<String> readXLSTarget(String fileDir){
        HashMap<String, Integer> src = this.readXLSSrc("public/target.xls");
        Set<String> srcKeys = src.keySet();
        ArrayList<String> nameTarget = new ArrayList<String>();
        try {
                Workbook book = Workbook.getWorkbook(new File("public/main.xls"));
            WritableWorkbook wwb = Workbook.createWorkbook(new File(fileDir), book);

            WritableSheet sheet = wwb.getSheet(0);

            for(int i=1;i<=196;i++){
                WritableCell cell1 = sheet.getWritableCell(0, i);

                String result = cell1.getContents();

                if(result.length()>0){
                    if(srcKeys.contains(result)){
                        System.out.println("in contains");
                            jxl.write.Label label = new jxl.write.Label(8,i,src.get(result).toString());
                        System.out.println("label"+label.getRow()+" "+label.getColumn()+" "+result);
//                      label.setCellFormat(cf);
                        sheet.addCell(label);
                    }
                    nameTarget.add(result);
                }
            }
            wwb.write();
            wwb.close();
            book.close();
        } catch (Exception e) {
            //System.out.println(e);
            e.printStackTrace();
        }
        return nameTarget;
    }

    public static void main(String[] args){
        ReadXLS readXLS = new ReadXLS();
//        ArrayList<String> src = readXLS.readXLSSrc("public/src.xls");
        ArrayList<String> target = readXLS.readXLSTarget("public/main1.xls");
        /*for(String name:src){
            if(target.contains(name)){
                System.out.println("contains"+name);
            }
            else
                System.out.println("untains"+name);
        }*/
    }
}
